﻿@using System.Data

@inherits SharedPage.Base.EchartsBase
@implements IAsyncDisposable
@if (ChartInfo != null)
{
    if (@IsDesigner)
    {
        switch (@ChartInfo.ComponentType)
        {
            case ComponentType.Line:
            case ComponentType.Pie:
                <div class="dragbox" tabindex="0" style="position:absolute; height:@ChartInfo.Height%;width:@ChartInfo.Width%;top:@ChartInfo.Top%;left:@ChartInfo.Left%" draggable="true"
                @onclick:stopPropagation @oncontextmenu:stopPropagation @oncontextmenu:preventDefault
                     @ondragstart="HandleDragStart"
                     @onmousemove="async(e)=>{        }"
                     @ondragend="HandleDragend"
                     @onclick="HandelClick"
                     @onkeydown="HandleKeyDown"
                     @oncontextmenu="async(e)=>{ //调用js程序,将一个右键组件显示出来,通过display属性 //await JsEcharts.Log(e);
       }">
                    <div id="@ChartInfo.Id" style="height:100%;width:100%"></div>
                </div>
                break;
            case ComponentType.Other:
                break;
            default:
                break;
        }

    }
    else
    {
        <div id="@ChartInfo.Id" style="position:absolute;height:@ChartInfo.Height%;width:@ChartInfo.Width%;top:@ChartInfo.Top%;left:@ChartInfo.Left%"></div>
    }
}
@code {
    [Parameter]
    [NotNull]
    public BigScreen? BigScreen { get; set; }

  

    [Parameter]
    [NotNull]
    public ComponentInfo? ChartInfo { get; set; } = new();

    [Parameter]
    public bool IsDesigner { get; set; }

    protected override Task OnInitializedAsync()
    {

        return base.OnInitializedAsync();
    }
    Random r = new();
    public async void SetOption1(DataTable? src)
    {
        if (ChartInfo != null && ChartInfo.Option != null && ChartInfo.Option.dataset != null)
        {
            ChartInfo.Option.dataset.source = src;
            await JsEcharts.SetOption(ChartInfo.Id, ChartInfo.Option);
            await InvokeAsync(this.StateHasChanged);
        }
    }
    public async void SetOption(DataTable? src)
    {
        // if (ChartInfo != null && ChartInfo.Option != null && ChartInfo.Option.dataset != null)
        // {
        //     if (ChartInfo.Option.dataset.source.Count > 10)
        //     {
        //         ChartInfo.Option.dataset.source.RemoveAt(0);
        //     }

        //     ChartInfo.Option.dataset.source.Add(src.Last());
        //     await JsEcharts.SetOption(ChartInfo.Id, ChartInfo.Option);
        //     await InvokeAsync(this.StateHasChanged);
        // }
    }
    public async void AddData(object[] data)
    {

        // ChartInfo.Option?.dataset?.source?.Add(data);
        // await JsEcharts.SetOption(ChartInfo.Id, ChartInfo.Option);
    }
    public async void RemoveData()
    {
        // ChartInfo.Option?.dataset?.source?.RemoveAt(0);
        // await JsEcharts.SetOption(ChartInfo.Id, ChartInfo.Option);
    }
    public async void RemoveAndAddData(object[] data)
    {
        // ChartInfo.Option?.dataset?.source?.RemoveAt(0);
        // ChartInfo.Option?.dataset?.source?.Add(data);
        // await JsEcharts.SetOption(ChartInfo.Id, ChartInfo.Option);
    }
    /// <summary>
    /// 组件被单击后
    /// </summary>
    /// <param name="e"></param>
    /// <returns></returns>
    async Task HandelClick(MouseEventArgs e)
    {
        if (e.CtrlKey)
        {
            if (BigScreen.SelectedList.Find(x => x.Id == ChartInfo.Id) == null)
            {
                BigScreen.SelectedList.Add(ChartInfo);
            }
        }
        else
        {
            BigScreen.SelectedList.Clear();
            BigScreen.SelectedList.Add(ChartInfo);
        }
        BigScreen.SelectedByDesigner = ChartInfo;
        await JsEcharts.addClassForSelect(ChartInfo.Id, e.CtrlKey);
        BigScreen?.RefreshHandel?.Invoke(false);//这里全局优化,实际是为了刷新属性面板
        BigScreen?.RefreshHandel?.Invoke(false);
    }
    /// <summary>
    /// 拖动开始
    /// </summary>
    /// <param name="e"></param>
    /// <returns></returns>
    async Task HandleDragStart(DragEventArgs e)
    {
        ChartInfo.MoveInfo.StartX = e.ScreenX;
        ChartInfo.MoveInfo.StartY = e.ScreenY;
        BigScreen.SelectedByDesigner = ChartInfo;
        BigScreen.SelectedList.Clear();
        BigScreen.SelectedList.Add(ChartInfo);
        await JsEcharts.addClassForSelect(ChartInfo.Id);
    }
    /// <summary>
    /// 拖动完成后
    /// </summary>
    /// <param name="e"></param>
    /// <returns></returns>
    async Task HandleDragend(DragEventArgs e)
    {
        var ss = await JsEcharts.getWH("designer");
        var w = ss[0];
        var h = ss[1];
        var offsetx = e.ScreenX - ChartInfo.MoveInfo.StartX;
        var offsety = e.ScreenY - ChartInfo.MoveInfo.StartY;
        ChartInfo.Top = ChartInfo.Top + Convert.ToSingle((offsety / h * 100));
        ChartInfo.Left = ChartInfo.Left + Convert.ToSingle((offsetx / w * 100));
        BigScreen?.RefreshHandel?.Invoke(false);//这里全局优化,实际是为了刷新属性面板
    }

    async Task HandleKeyDown(KeyboardEventArgs e)
    {
        switch (e.Key)
        {
            case "ArrowUp":
                BigScreen.SelectedList.ForEach(x => x.Top--);
                break;
            case "ArrowDown":
                BigScreen.SelectedList.ForEach(x => x.Top++);
                break;
            case "ArrowLeft":
                BigScreen.SelectedList.ForEach(x => x.Left--);
                break;
            case "ArrowRight":
                BigScreen.SelectedList.ForEach(x => x.Left++);
                break;
            case "Delete":
                if (BigScreen.SelectedByDesigner!=null)
                {
                    await JsEcharts.dispose(BigScreen.SelectedByDesigner.Id);
                    BigScreen.ChartList.RemoveAll(x => x.Id == BigScreen.SelectedByDesigner.Id);//清除了
                    BigScreen.SelectedList.Clear();

                    BigScreen.SelectedByDesigner = null;
                }
                // BigScreen.SelectedList.ForEach(async (x) =>
                // {
                //     await JsEcharts.dispose(x.Id);
                // });
                BigScreen?.RefreshHandel?.Invoke(false);
                await JsEcharts.removeClassForSelect();
                break;
            case "c":
            case "C":
                if (e.CtrlKey && BigScreen.SelectedList.Count > 0)
                {
                    var obj = BigScreen.SelectedList.Select(x => x.Clone());
                    //优化,直接放到剪贴板中
                    await JsEcharts.Copy(JsonSerializer.Serialize(obj));
                    BigScreen.CopyList.Clear();
                    BigScreen.CopyList.AddRange(obj);
                }
                break;
            case "v":
            case "V":
                if (e.CtrlKey && BigScreen.CopyList.Count > 0)
                {
                    //1.复制一份剪贴板中的数据出来
                    var objs = BigScreen.CopyList.Select(x => x.Clone()).ToList();
                    if (objs != null)//当前页面有被复制对象
                    {
                        BigScreen.ChartList.AddRange(objs);//取消下面,是因为性能,我没有批量增加的方法,所以需要多次调用addClassForSelect
                                                           //BigScreen.SelectedList.Clear();
                                                           //BigScreen.SelectedList.AddRange(objs);
                                                           //BigScreen.Selected = null;
                                                           //await JsEcharts.addClassForSelect(ChartInfo.Id, e.CtrlKey);
                    }
                    else if (true)//没有,检查剪贴板的
                    {

                    }
                    BigScreen?.RefreshHandel?.Invoke(false);
                }
                break;

        }
        if (BigScreen.SelectedList.Count > 1)
        {
            
            BigScreen?.RefreshHandel?.Invoke(false);
        }
    }
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (firstRender)
        {
            await JsEcharts.Init(ChartInfo.Id);
            await JsEcharts.SetOption(ChartInfo.Id, ChartInfo.Option);
            await JsEcharts.addResizeListener(ChartInfo.Id);
            ChartInfo.SetOption = SetOption;
        }

    }
    public async ValueTask DisposeAsync()
    {
        await JsEcharts.removeResizeListener(ChartInfo.Id);
        GC.SuppressFinalize(this);
    }

}
